package com.aipolice.web;


import com.aipolice.pojo.Conreport;
import com.aipolice.pojo.Report;
import com.aipolice.service.ConreportService;
import com.aipolice.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
public class ReportController {
    @Autowired
    ReportService reportService;

    @Autowired
    ConreportService conreportService;

    @GetMapping("/reports")
    public List<Report> users(){
        return reportService.list();

    }

    @PostMapping("/addreport")
    public Report add(Report bean, String content,HttpServletRequest request) throws IOException {
        System.out.println(content);
        //正文加入数据库
        Conreport conreport = addConreport(content);
        bean.setConid(conreport.getId());
        System.out.println("!!!!!!!!!!!!!!!!!!Report"+bean.getLevel());
        addReport(bean,request);
        return bean;
    }

    @PostMapping("/reports/{id}/{state}")
    public void delete(@PathVariable("id") int id,@PathVariable("state") int state){
        Report report = reportService.find(id);
        report.setState(state);
        System.out.println("更新state");
        reportService.add(report);
    }

    public Report addReport(Report bean,HttpServletRequest request) throws IOException {

        Date date = new Date();
        Timestamp nousedate = new Timestamp(date.getTime());
        bean.setDate(nousedate);
        System.out.println(bean.toString());
        reportService.add(bean);
        System.out.println(bean.getId());
        return bean;
    }

    public Conreport addConreport(String content){
        Conreport conreport = new Conreport();
        conreport.setContent(content);
        conreportService.add(conreport);
        return  conreport;
    }

    //获得文章正文
    @GetMapping("/report/{id}")
    public Conreport get(@PathVariable("id") int id){
        System.out.println("test!!!!!"+id);
        return conreportService.find(id);
    }



    @DeleteMapping("/reports/{id}")
    public void delete(@PathVariable("id") int id){
        System.out.println("删除方法");
        reportService.delete(id);
    }



}
